class Order < ApplicationRecord
  belongs_to :member
  belongs_to :company
  belongs_to :store
  has_one :appointment, dependent: :destroy
  has_one :timetable, through: :appointment
  has_one :coach, through: :timetable
  has_one :running_account, class_name: "Transaction", foreign_key: :order_no, primary_key: :order_no
  enum status: {paid_hidden: -4, rfd_hidden: -3, cl_hidden: -2, canceled: -1, pending: 0, paid: 1, refunded: 2}

  before_save {false}

  module UnionHack
    def union(*relations)
      from '((' + relations.map {|r| r.to_sql}.join(') UNION (') + ')) AS ' + self.table_name
    end
  end
  extend UnionHack
  ransacker :mobile, formmater: proc {|selected_mobile|
    results = Order.where('mobile like %?%', selected_mobile)
    results.present? ? results : nil
  }

  def promotion_info_detail
    if promotion_info.present?
      <<-EOF
        编号:#{promotion_info.fetch("id", '')}
        活动:#{promotion_info.fetch("name", '')}
        类型:#{promotion_info.fetch("type", '')}
      EOF
    end
  end

  def experience_info_detail
    if experience_info.present?
      <<-EOF
      编号:#{experience_info.fetch("id", '')}
      活动:#{experience_info.fetch("name", '')}
      EOF
    end
  end
end
